package com.efounder.form.comp;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.DataSetListener;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.form.DMColComponent;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.model.JSONFormModel;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.frame.utils.EFFrameUtils;
import com.efounder.mobilecomps.R;
import com.efounder.util.AppContext;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.picker.DateTimePicker;

/**
 * Created by lch on 16/9/2.
 * 日期组件
 */
public class FormYMDHMText extends TextView implements IComponent,IUIComponent,DMComponent,DMColComponent,DataSetListener{

	private String originalText;
	private Boolean editable = true;

	private int gap;

	private String dataSetID;
	private String dataSetColID;

	private String horizontalAlign;
	private String verticalAlign;

	private DataSetComponent dataSetComponent;
	private EFDataSet dataSet;
	private EFRowSet mainRowSet;

	private ViewSize size = new ViewSize(0, 0);


	private Context context;
	private Calendar calendar = Calendar.getInstance();

	private IUIComponent parentComp;
	enum MODEL{YEAR_MONTH_DAY ,HOUR_OF_DAY};
	MODEL rqModel;


	public FormYMDHMText() {
		super(AppContext.getCurrentActivity());
		this.context = AppContext.getCurrentActivity();
		this.setTextColor(Color.BLACK);
	}

	public FormYMDHMText(Context context) {
		super(context);
		this.context = AppContext.getCurrentActivity();
	}
	public FormYMDHMText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	private void showToast(String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}


	public void dataChanged(EFDataSet dataSet) {
		mainRowSet = dataSet.getCurrentRowSet();
		String value = "";
		if (mainRowSet != null) {
			value = mainRowSet.getString(dataSetColID, "");
		}
		if ("".equals(value)){
			SimpleDateFormat sdf = new SimpleDateFormat(this.getDateFormat());
			String dateStr = sdf.format(calendar.getTime());
			mainRowSet.putString(dataSetColID,dateStr);
		}else {
			this.setText(value);
		}
	}

	public String getOriginalText() {
		return originalText;
	}

	public void setOriginalText(String originalText) {
		this.originalText = originalText;

		this.setText(originalText);
	}

	@Override
	public DataSetComponent getDataSetComponent() {
		return dataSetComponent;
	}

	@Override
	public void setDataSetComponent(DataSetComponent dsc) {
		if (dataSetComponent != null)
			dataSetComponent.removeDMComponent(this);
		dataSetComponent = dsc;
		if (dataSetComponent != null)
			dataSetComponent.insertDMComponent(this);
	}

    public void setDataSetComponent(JSONFormModel dsc) {
        if (dataSetComponent != null)
            dataSetComponent.removeDMComponent(this);
        dataSetComponent = dsc;
        if (dataSetComponent != null)
            dataSetComponent.insertDMComponent(this);
    }

	@Override
	public void setLayout() {
		FormCompUtil.setLayoutSize(this);
		this.setGravity(FormCompUtil.getLayoutGravity(horizontalAlign,verticalAlign));

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParams.width= this.getViewSize().getWidth();
		layoutParams.height = this.getViewSize().getHeight();
		this.setLayoutParams(layoutParams);
	}

	@Override
	public void creationComplete() {


//		Drawable bottomDrawable = getResources().getDrawable(R.drawable.textviewbottomline);
//		bottomDrawable.setBounds(0, 0, this.getViewSize().getWidth()+100, bottomDrawable.getMinimumHeight());

		Drawable rightDrawable = getResources().getDrawable(R.drawable.icon_time);
		rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
		Drawable leftDrawable = getResources().getDrawable(R.drawable.ic_launcher);
		leftDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
		this.setCompoundDrawables(null,null,rightDrawable,null);
		//this.setCompoundDrawablesWithIntrinsicBounds(null,null,null,rightDrawable);
		this.setBackgroundResource(R.drawable.ymdeditbackground);
		this.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
		this.setPadding(30,0,0,0);
		this.setTextColor(getResources().getColor(R.color.black));
		//日期样式格式化
		if(FormYMDHMText.this.getDateFormat().equals("yyyy-MM-dd HH:mm:ss")) {
			//时分秒样式
			rqModel = MODEL.HOUR_OF_DAY;
		}else{
			//年月日样式
			rqModel = MODEL.YEAR_MONTH_DAY;
		}
		this.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!editable) return;
					switch (rqModel){
					case HOUR_OF_DAY:
						DateTimePicker	picker = new DateTimePicker(AppContext.getCurrentActivity(), DateTimePicker.HOUR_OF_DAY);

						picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
							@Override
							public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
								String str=year + "-" + month + "-" + day + " " + hour + ":" + minute;

								SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm");

								try {
									Date date = sdf.parse(str);
									String dateStr = sdf.format(date);

									if(mainRowSet!=null){
//							mainRowSet.putString(dataSetColID,year + "-" + month + "-" + day + " " + hour + ":" + minute);
										mainRowSet.putString(dataSetColID,dateStr);
									}
								} catch (ParseException e) {
									e.printStackTrace();
								}

							}
						});
						picker.setRange(calendar.get(Calendar.YEAR)-20, calendar.get(Calendar.YEAR)+20);
						picker.setSelectedItem(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH),
								calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
						picker.show();
						break;
					case YEAR_MONTH_DAY:
						DatePicker	ymdPicker = new DatePicker(AppContext.getCurrentActivity(), DatePicker.YEAR_MONTH_DAY);

						ymdPicker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
							@Override
							public void onDatePicked(String year, String month, String day) {
								String str=year + "-" + month + "-" + day;

								SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

								try {
									Date date = sdf.parse(str);
									String dateStr = new SimpleDateFormat(dateFormat).format(date);

									if(mainRowSet!=null){
//							mainRowSet.putString(dataSetColID,year + "-" + month + "-" + day + " " + hour + ":" + minute);
										mainRowSet.putString(dataSetColID,dateStr);
									}
								} catch (ParseException e) {
									e.printStackTrace();
								}
							}
						});
						ymdPicker.setRange(calendar.get(Calendar.YEAR)-20, calendar.get(Calendar.YEAR)+20);
						ymdPicker.setSelectedItem(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
						ymdPicker.show();
						break;
				}



			}
		});

	}

	public String getHorizontalAlign() {
		return horizontalAlign;
	}

	public void setHorizontalAlign(String horizontalAlign) {
		this.horizontalAlign = horizontalAlign;
	}

	public String getVerticalAlign() {
		return verticalAlign;
	}

	public void setVerticalAlign(String verticalAlign) {
		this.verticalAlign = verticalAlign;
	}

	@Override
	public void setDataSetID(String dataSetID) {
		this.dataSetID = dataSetID;
	}

	@Override
	public String getDataSetColID() {
		return dataSetColID;
	}

	@Override
	public void setDataSetColID(String dataSetColID) {
		this.dataSetColID = dataSetColID;
	}

	@Override
	public String getDataSetNameColID() {
		return null;
	}

	@Override
	public void setDataSetNameColID(String value) {

	}

	@Override
	public String getInternalDataSetID() {
		return null;
	}

	@Override
	public void setInternalDataSetID(String dataSetID) {

	}

	@Override
	public void setViewDataSetID(String viewDataSetID) {

	}

	@Override
	public String getViewDataSetID() {
		return null;
	}

	@Override
	public void setViewDataSetColID(String viewDataSetColID) {

	}

	@Override
	public String getViewDataSetColID() {
		return null;
	}

	@Override
	public void setValueDataSetColID(String valueDataSetColID) {

	}

	@Override
	public String getValueDataSetColID() {
		return null;
	}

	@Override
	public Boolean getIsUserInternalDataSetID() {
		return null;
	}

	@Override
	public void setIsUserInternalDataSetID(Boolean v) {

	}

	@Override
	public String getIdentifier() {
		return null;
	}

	@Override
	public Object getPropertyMap() {
		return null;
	}

	@Override
	public void setPropertyMap(Object value) {

	}

	@Override
	public Object getProperty(String key, Object defVal) {
		return null;
	}

	@Override
	public void setProperty(String key, Object value) {

	}

	@Override
	public int getNumberPrecision() {
		return 0;
	}

	@Override
	public void seNumberPrecision(int value) {

	}

	@Override
	public String getNumberFormat() {
		return null;
	}

	@Override
	public void setNumberFormat(String numberFormat) {

	}

	@Override
	public String getColumnType() {
		return null;
	}

	@Override
	public void setColumnType(String value) {

	}

	@Override
	public String getEditorType() {
		return null;
	}

	@Override
	public void setEditorType(String value) {

	}

	@Override
	public String getDateFormat() {
			return dateFormat;
	}

	@Override
	public void setDateFormat(String _dateFormat) {
		if(_dateFormat.equals("YYYY-MM-DD JJ:NN:SS"))
			_dateFormat = "yyyyMMdd";
       dateFormat = _dateFormat;
	}

	@Override
	public String getFormulaOne() {
		return null;
	}

	@Override
	public void setFormulaOne(String formulaOne) {

	}

	@Override
	public String getEditMask() {
		return null;
	}

	@Override
	public void setEditMask(String editMask) {

	}

	@Override
	public String getFkeyID() {
		return null;
	}

	@Override
	public void setFkeyID(String fkey) {

	}

	@Override
	public String getRlglID() {
		return null;
	}

	@Override
	public void setRlglID(String rlglID) {

	}

	@Override
	public String getDataSetID() {
		return dataSetID;
	}

	@Override
	public void setDataSet(EFDataSet dataSet) {
		if ( this.dataSet != dataSet ) {
			if ( this.dataSet != null ) this.dataSet.removeDataSetListener(this);
			this.dataSet = dataSet;
			if ( this.dataSet != null ) this.dataSet.addDataSetListener(this);
			this.dataChanged(dataSet);
		}

	}

	@Override
	public EFDataSet getDataSet() {
		return dataSet;
	}

	@Override
	public EFRowSet getMainRowSet() {
		return mainRowSet;
	}

	@Override
	public void dataSetChanged(DataSetEvent event) {
		dataChanged(event.getDataSet());
	}

	@Override
	public String getID() {
		return id;
	}

	@Override
	public void setID(String id) {
		this.id = id;
	}

	@Override
	public int getGap() {
		return gap;
	}

	@Override
	public void setGap(int gap) {
		this.gap = gap;
	}

	@Override
	public void setWidth(int width) {
		this.width = width;
	}

	@Override
	public void setHeight(int height) {
		this.height = height;
	}

	@Override
	public int getPercentWidth() {
		return percentWidth;
	}

	@Override
	public void setPercentWidth(int percentW) {
		percentWidth = percentW;
	}

	@Override
	public int getPercentHeight() {
		return percentHeight;
	}

	@Override
	public void setPercentHeight(int percentH) {
		percentHeight = percentH;
	}

	@Override
	public ViewSize getViewSize() {
		return size;
	}

	@Override
	public void setViewSize(ViewSize size) {
		this.size = size;
	}

	@Override
	public void setCompWidth(int width) {
		this.width = width;
	}

	@Override
	public int getCompWidth(){
		return this.width;
	}

	@Override
	public void setCompHeight(int height) {
		this.height = height;
	}

	@Override
	public int getCompHeight() {
		return this.height;
	}

	private String id;
	private int x = 0;
	private int y = 0;
	private boolean enabled;
	private String toolTip;
	private boolean visible;
	private int width;
	private int height;
	private int percentWidth;
	private int percentHeight;
	private String dateFormat;

	public void setId(String id) {
		this.id = id;
	}



	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}




	public Boolean getEnabled() {
		return enabled;
	}

	public String getToolTip(String a) {
		return toolTip;
	}

	public Boolean getVisible() {
		return visible;
	}

	public void setVisible(Boolean visible) {
		this.visible = visible;
	}



	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
		super.setEnabled(enabled);
	}

	public void setToolTip(String toolTip) {
		this.toolTip = toolTip;
	}




	//不可输入状态时，确定是否点击整个组件下拉 true->点击整个组件下拉
	private boolean isClickInputOpenList = false;
	public boolean isClickInputOpenList() {
		return isClickInputOpenList;
	}

	@Override
	public IUIComponent getParentComp() {
		return parentComp;
	}

	@Override
	public void setParentComp(IUIComponent comp) {
		parentComp = comp;
	}


	public Boolean getEditable() {
		return editable;
	}

	public void setEditable(Boolean editable) {
		this.editable = editable;
	}
	// ----------------------------------------------------------------------------------------------------------------------------------
	// 结束：组件公共属性
	// ----------------------------------------------------------------------------------------------------------------------------------




}